﻿using Minecraft.BLL.mysql;
using Minecraft.Config;
using Minecraft.Model;
using Minecraft.Model.Backpack;
using Minecraft.Model.ReqResp;
using SuperSocket.SocketBase.Command;
using SuperSocket.SocketBase.Protocol;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;

namespace Minecraft.ServerHall.Cmd
{
	public class BackpackGoodsSelect : CommandBase<MinecraftSession, StringRequestInfo>
	{
		public override string Name
		{
			get
			{
				return ProtocolHelper.GetProtocolStr(defCommand);
			}
		}
		private EnumCommand defCommand = EnumCommand.Backpack_BackpackGoodsSelect;
		public override void ExecuteCommand(MinecraftSession session, StringRequestInfo requestInfo)
		{
			var req = requestInfo.GetRequestObj<BackpackGoodsSelectReq>(session);
			if (req == null)
			{
				session.Send(defCommand, req, new BaseResp { RespLevel = RespLevelEnum.Error, Msg = "错误1：（原因：请求实体为空）" });
				return;
			}
			if (!session.minecraftSessionInfo.IsLogin)
			{
				session.Send(defCommand, req, new BaseResp { RespLevel = RespLevelEnum.Error, Msg = "错误1：（原因：未登录）" });
				return;
			}
			//物品来源判断
			if (!MinecraftConfig.enumGoodsSourceBelongsToTypeList.Any(m => m == req.enumGoodsBelongsToType))
			{
				session.Send(defCommand, req, new BaseResp { RespLevel = RespLevelEnum.Error, Msg = $"错误1：（原因：物品来源不正确，前端传入的物品来源为【{req.enumGoodsBelongsToType.ToString()}】）" });
				return;
			}
			if (req.guid.IsNullOrWhiteSpace())
			{
				req.guid = "";
			}
			var goodsBelongsTypeList = new List<EnumGoodsBelongsToType>();
			if (MinecraftConfig.bpShortbarBelongsToTypeList.Any(m => m == req.enumGoodsBelongsToType))
			{
				goodsBelongsTypeList = MinecraftConfig.bpShortbarBelongsToTypeList;
				req.guid = "";
			}
			else
			{
				//对guid是否存在做判断
				switch (req.enumGoodsBelongsToType)
				{
					case EnumGoodsBelongsToType.FurnitureStorage:
						{
							var storageFurnitureInfo = session.minecraftSessionInfo.furnitureStorageList.FirstOrDefault(m => m.FurnitureId == req.guid);
							if (storageFurnitureInfo == null)
							{
								session.Send(defCommand, req, new BaseResp() { RespLevel = RespLevelEnum.Error, Msg = $"传入的guid为【{req.guid}的存储家具数据库信息不存在】" });
								return;
							}
							var mod_FurnitureInfo = storageFurnitureInfo.GetMod_FurnitureInfo();
							if (mod_FurnitureInfo == null)
							{
								session.Send(defCommand, req, new BaseResp() { RespLevel = RespLevelEnum.Error, Msg = $"传入的guid为【{req.guid}的存储家具配置表信息不存在，数据异常】" });
								return;
							}
							if (mod_FurnitureInfo.enumFurnitureType_Convert != EnumFurnitureType.Storage)
							{
								session.Send(defCommand, req, new BaseResp() { RespLevel = RespLevelEnum.Error, Msg = $"传入的guid为【{req.guid}的家具类型不是存储家具类型】" });
								return;
							}
						}
						break;
					case EnumGoodsBelongsToType.Treasurebox:
						{

							////暂时写死
							//req.guid = session.minecraftSessionInfo.furnitureTreasureBoxList.FirstOrDefault(m => m.TreasureBox_MapId == session.minecraftSessionInfo.map.SourceMapId && m.TreasureBox_IsPrefab_Convert == false).FurnitureId;


							var furnitureTreasureInfo = session.minecraftSessionInfo.furnitureTreasureBoxList.FirstOrDefault(m => m.FurnitureId == req.guid);
							if (furnitureTreasureInfo == null)
							{
								session.Send(defCommand, req, new BaseResp() { RespLevel = RespLevelEnum.Error, Msg = $"传入的guid为【{req.guid}】的宝箱数据库信息不存在" });
								return;
							}
							var mod_FurnitureInfo = furnitureTreasureInfo.GetMod_FurnitureInfo();
							if (mod_FurnitureInfo == null)
							{
								session.Send(defCommand, req, new BaseResp() { RespLevel = RespLevelEnum.Error, Msg = $"传入的guid为【{req.guid}】的宝箱配置表信息不存在，数据异常" });
								return;
							}
							if (mod_FurnitureInfo.enumFurnitureType_Convert != EnumFurnitureType.Treasurebox)
							{
								session.Send(defCommand, req, new BaseResp() { RespLevel = RespLevelEnum.Error, Msg = $"传入的guid为【{req.guid}】的家具类型不是宝箱类型" });
								return;
							}
						}
						break;
					default:
						{
							session.Send(defCommand, req, new BaseResp { RespLevel = RespLevelEnum.Error, Msg = $"错误1：（原因：暂不支持物品来源【{req.enumGoodsBelongsToType.ToString()}】）" });
							return;
						}
				}
				goodsBelongsTypeList = new List<EnumGoodsBelongsToType>() { req.enumGoodsBelongsToType };
			}
			//给客户端物品信息
			var cellList = session.minecraftSessionInfo.goodsList.Where(m => goodsBelongsTypeList.Any(k => k == m.enumGoodsBelongsToType_Convert) && m.guid == req.guid).GetBackpackGoodsCellInfo();

			var resp = new BackpackGoodsSelectResp
			{
				CellList = cellList,
				enumGoodsBelongsToType = req.enumGoodsBelongsToType,
				guid = req.guid,
				entityId = req.entityId
			};
			session.Send(defCommand, req, resp);
		}
	}
}
